<template>
  <div>
    <el-card class="box-card">
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item label="">
          <el-input placeholder="关键字"></el-input>
        </el-form-item>
        <el-form-item label="">
          <el-button type="primary" icon="el-icon-search">搜索</el-button>
        </el-form-item>
      </el-form>
      <div>
        <el-button type="primary" icon="el-icon-plus" @click="handleAdd"
          >添加</el-button
        >
        <el-button type="primary" icon="el-icon-refresh" @click="handleRefresh"
          >刷新</el-button
        >
      </div>
    </el-card>
    <el-card>
      <el-table
        :data="tbData"
        style="width: 100%"
        v-loading="loading"
        element-loading-text="数据正在拼命赶来的路上。。。"
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(0, 0, 0, 0.8)"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column label="序号" width="60">
          <template slot-scope="abc">
            {{ abc.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column property="cateName" label="品类名称">
        </el-table-column>
        <el-table-column property="isActived" label="是否启用">
          <template slot-scope="scope">
            <span>{{ scope.row.isActived ? "启用" : "禁用" }}</span>
          </template>
        </el-table-column>
        <el-table-column label="更新时间" property="updatedTime">
        </el-table-column>
        <el-table-column label="备注" property="remarks">
          <div class="slh" slot-scope="scope">
            <el-popover trigger="hover" placement="top">
              <p>住址: {{ scope.row.remarks }}</p>
              <div slot="reference" class="name-wrapper">
                <el-tag size="mini">{{ scope.row.remarks }}</el-tag>
              </div>
            </el-popover>
          </div>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleEdit(scope.$index, scope.row)"
              icon="el-icon-edit"
            ></el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              icon="el-icon-delete"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <div>
        <el-dialog title="文章分类" :visible.sync="dialogFormVisible">
          <el-form :model="form" inline :rules="rules" ref="formDemo">
            <el-form-item label="活动名称" prop="cateName">
              <el-input v-model="form.cateName" autocomplete="off"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="handleCancel">取 消</el-button>
            <el-button type="primary" @click="handleSave">确 定</el-button>
          </div>
        </el-dialog>
      </div>
    </el-card>
  </div>
</template>
<script>
import axios from "axios";
export default {
  data() {
    return {
      currentEditIndex: 0,
      rules: {
        cateName: [
          {
            required: true,
            trigger: "blur",
            message: "请输入文章分类名称",
          },
        ],
      },
      loading: false,
      tbData: [],
      dialogFormVisible: false,
      form: {
        id: "",
        cateName: "",
      },
    };
  },
  methods: {
    handleAdd() {
      // 弹出对话框时，当还没有初始化完成就调用resetFields方法会出现问题，所以我们在下一轮轮询时，再来做这个动作，可以避免找不到方法
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs.formDemo.resetFields();
      });
    },
    handleEdit(index, row) {
      // 弹出对话框时，当还没有初始化完成就调用resetFields方法会出现问题，所以我们在下一轮轮询时，再来做这个动作，可以避免找不到方法
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs.formDemo.resetFields();
        // 赋值也一样，还没有重置完成，会导致其中的赋值不会成功
        this.currentEditIndex = index;
        this.form.id = row.id;
        this.form.cateName = row.cateName;
      });
    },
    handleDelete(index, row) {
      console.log(index, row);
      axios.delete(`/article/category/${row.id}`).then(({ data }) => {
        console.log(data);
        this.tbData = this.tbData.filter((item) => {
          return item.id !== data.data.id;
        });
      });
    },
    getData() {
      axios.get("/article/category").then(({ data }) => {
        console.log(data);
        this.tbData = data.data;
      });
    },
    handleRefresh() {
      this.getData();
      // console.log("刷新数据成功");
      this.$message.success("刷新数据成功");
    },

    handleCancel() {},
    handleSave() {
      console.log(this.form);
      // 如果id存在并且长度大于0，则认为是修改后的保存，否则为新增
      if (this.form.id && this.form.id.length > 0) {
        axios.put("/article/category", this.form).then(({ data }) => {
          console.log(data);
          this.tbData.splice(this.currentEditIndex, 1, data.data);
          // this.$refs.formDemo.resetFields();
          console.log(this.tbData);
        });
      } else {
        axios.post("/article/category", this.form).then(({ data }) => {
          console.log(data);
          this.tbData.push(data.data);

          // this.$refs.formDemo.resetFields();
        });
      }
      this.dialogFormVisible = false;
    },
  },
  created: function () {
    this.getData();
  },
};
</script>
<style>
.el-card__body {
  display: flex;
  justify-content: space-between;
}
.el-table th.el-table__cell > .cell {
  padding-left: 14px !important;
}
.slh {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
</style>